package com.guli.cart.controller;

import com.guli.cart.conf.CartInterceptor;
import com.guli.cart.service.CartService;
import com.guli.cart.vo.CartItemVo;
import com.guli.cart.vo.CartVo;
import com.guli.cart.vo.UserVo;
import com.guli.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WZH
 * @title: CartController
 * @projectName gulimall-java
 * @description:
 * @date 2022/1/17
 */
@Controller
public class CartController {
    @Autowired
    private CartService cartService;
//购物车页面
    @GetMapping("/")
    public String getCart(Model model){
        CartVo cartVo=cartService.getCartVo();
        model.addAttribute("cartVo",cartVo);
        return "cartList";
    }
    //添加购物车
    @PostMapping("/addToCart")
    public String addToCart(@RequestParam String skuId,@RequestParam Integer num){
        cartService.addToCart(skuId,num);
        return "redirect:http://cart.gulimall.com/success.html?skuId="+skuId;
    }
//跳转成功页面
    @GetMapping("/success.html")
    public String success(@RequestParam String skuId, Model model){
        CartItemVo cartItemVo = cartService.getCart(skuId);
        model.addAttribute("cart",cartItemVo);
        return "success";
    }
//修改选中状态
    @GetMapping("/updCheck")
    public String updCheck(@RequestParam Long skuId,@RequestParam Boolean check){
        cartService.updCheck(skuId,check);
        return "redirect:http://cart.gulimall.com";
    }
    //修改数量
    @GetMapping("/updNum")
    public String updNum(@RequestParam Long skuId,@RequestParam Integer num){
        cartService.updNum(skuId,num);
        return "redirect:http://cart.gulimall.com";
    }

    //删除产品
    @GetMapping("/del")
    public String del(@RequestParam Long skuId){
        cartService.del(skuId);
        return "redirect:http://cart.gulimall.com";
    }

    //查询选中的商品
    @GetMapping("/getCartCheckList")
    @ResponseBody
    public R getCartCheckList(){
        return cartService.getCartCheckList();
    }
}
